<template>
  <basic-container v-loading="loading">
    <div class="header bgd-f6">{{title(type)}}</div>
    <div class="header bgd-f6" style="border-top:none">{{query.nameTitle}}</div>
    <div class="header bgd-f6" style="border-bottom:1px solid #ccc;border-top:none" v-if="query.dayProfit">{{query.dayProfit}}</div>
    <div class="header bgd-f6" style="border-bottom:1px solid #ccc;border-top:none" v-if="query.monthProfit">{{query.monthProfit}}</div>
    <div class="line">
      <div class="bgd-f6 line-time ">通道</div>
      <div class="bgd-f6 line-time ">支付方式</div>
      <div class="bgd-f6 line-time " v-if="type=='childMonth'">结算方式</div>
      <div class="bgd-f6 line-time ">订单金额</div>
      <div class="bgd-f6 line-time ">交易笔数</div>
      <div class="bgd-f6 line-time ">交易手续费</div>
      <div class="bgd-f6 line-time ">垫资手续费</div>
      <div class="bgd-f6 line-time ">本级分润</div>
      <div class="bgd-f6 line-time ">本级垫资分润</div>
      <div class="bgd-f6 line-time " v-if="type=='childDay'||type=='childMonth'">机构分润</div>
      <div class="bgd-f6 line-time " v-if="type=='childDay'||type=='childMonth'">机构垫资分润</div>
      <div class="bgd-f6 line-time ">机构总分润</div>
    </div>
    <div class="line" v-for="(item,index) in dataList" :key="index">
      <div class="line-time bgd-f6">{{item.aisleName}}</div><!-- 通道 -->
      <div class="line-time ">{{item.prodName}}</div><!-- 支付方式 -->
      <div class="line-time " v-if="type=='childMonth'">{{item.settlementModeName}}</div> <!-- 结算方式 -->
      <div class="line-time ">{{exhibition(item.tradeAmount)}}</div><!-- 交易金额（元） -->
      <div class="line-time ">{{exhibition(item.tradeCount)}}</div><!-- 交易笔数 -->
      <div class="line-time ">{{exhibition(item.totalHandling)}}</div><!-- 手续费（元） -->
      <div class="line-time ">{{exhibition(item.totalHandlingD0)}}</div><!-- D0手续费（元） -->
      <div class="line-time ">{{exhibition(item.handlingProfit)}}</div><!-- 分润金额（元） -->
      <div class="line-time ">{{exhibition(item.handlingProfitD0)}}</div><!-- D0分润（元） -->
      <div class="line-time " v-if="type=='childDay'||type=='childMonth'">{{exhibition(item.childHandlingProfit)}}</div><!-- 下级手续费分润（元） -->
      <div class="line-time " v-if="type=='childDay'||type=='childMonth'">{{exhibition(item.childHandlingProfitD0)}}</div><!-- 下级D0分润（元） -->
      <div class="line-time ">{{exhibition(item.allProfitAmount)}}</div><!-- 总分润 -->
    </div>
    <div class="line">
      <div class="bgd-f6 line-time ">合计</div>
      <div class="line-time ">----</div>
      <div class="line-time " v-if="type=='childMonth'">----</div><!--  结算方式 -->
      <div class="line-time ">{{exhibition(countData.totalTradAmount)}}</div><!-- 交易金额（元） -->
      <div class="line-time ">{{exhibition(countData.tradeCount)}}</div><!-- 交易笔数 -->
      <div class="line-time ">{{exhibition(countData.totalRFee)}}</div><!-- 手续费（元） -->
      <div class="line-time ">{{exhibition(countData.totalRFeeD0)}}</div><!-- D0手续费（元） -->
      <div class="line-time ">{{exhibition(countData.totalProfitAmount)}}</div><!-- 分润金额（元） -->
      <div class="line-time ">{{exhibition(countData.totalProfitAmountD0)}}</div><!-- D0分润（元） -->
      <div class="line-time " v-if="type=='childDay'||type=='childMonth'">{{exhibition(countData.totalChildProfitAmount)}}</div><!-- 下级手续费分润（元） -->
      <div class="line-time " v-if="type=='childDay'||type=='childMonth'">{{exhibition(countData.totalChildProfitAmountD0)}}</div><!-- 下级D0分润（元） -->
      <div class="line-time ">{{exhibition(countData.allProfitAmount)}}</div><!-- 总分润 -->
    </div>
    
    <el-button type="primary" class="btnFooter" @click.stop="handleExport">下载{{title(type)}}</el-button>
  </basic-container>
</template>

<script>
import {getList as dayList ,getListCount as dayCount, getExport as dayExport} from "@/api/rb-service/oemprofit/service_profit_day";
import {getList as monthList ,getListCount as monthCount, getExport as monthExport} from "@/api/rb-service/oemprofit/service_profit_month";
import {getList as childDayList ,getListCount as childDayCount, getExport as childDayExport} from "@/api/rb-service/childOemProfit/service_profit_day";
import {getList as childMonthList ,getListCount as childMonthCount, getExport as childMonthExport} from "@/api/rb-service/childOemProfit/service_profit_month";
import {getList as allDayList ,getListCount as allDayCount, getExport as allDayExport} from "@/api/rb-service/serviceprofit/service_profit_day";
import {getList as allMonthList ,getListCount as allMonthCount, getExport as allMonthExport} from "@/api/rb-service/serviceprofit/service_profit_month";
import {getList as merDayList ,getListCount as merDayCount, getExport as merDayExport} from "@/api/rb-service/merchantprofit/service_profit_day";
import {getList as merMonthList ,getListCount as merMonthCount, getExport as merMonthExport} from "@/api/rb-service/merchantprofit/service_profit_month";

import { downloadFileExcel } from "@/util/util";

export default {
  data() {
    return {
      type:'',
      loading: true,
      query:{},//查询
      dataList:{},//查询结果
      countData:{},//统计
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    exhibition() {
      return (val) => {
        if (val == "" || val == "-1" || val == undefined) {
          return (val = '0.00');
        } else {
          return val;
        }
      };
    },
    // 标题
    title(){
      return (type) => {
        if(type == 'day'){
          return '本级日汇总报表';
        }else if(type == 'month'){
          return '本级月分润汇总报表';
        }else if(type == 'childDay'){
          return '机构日分润汇总报表';
        }else if(type == 'childMonth'){
          return '机构月分润汇总报表';
        }else if(type == 'alldDay'){
          return '服务商日分润汇总报表';
        }else if(type == 'allMonth'){
          return '服务商月分润汇总报表';
        }else if(type == 'merDay'){
          return '商户日分润汇总报表';
        }else if(type == 'merMonth'){
          return '商户月分润汇总报表';
        }else{
          return '分润汇总报表';
        }
      };
    },
    getTitle(){
      return (type) => {
        if(type == 'day'){
          return '本级日汇总';
        }else if(type == 'month'){
          return '本级月汇总';
        }else if(type == 'childDay'){
          return '机构日汇总';
        }else if(type == 'childMonth'){
          return '机构月汇总';
        }else if(type == 'alldDay'){
          return '服务商日汇总表';
        }else if(type == 'allMonth'){
          return '服务商月汇总';
        }else if(type == 'merDay'){
          return '商户日汇总';
        }else if(type == 'merMonth'){
          return '商户月汇总';
        }else{
          return '';
        }
      };
    }
  },
  created(){
    this.type = this.$route.query.type;
    this.onLoad()
  },
  methods: {
    /* 返回页面 */
    goBack() {
      this.$router.$avueRouter.closeTag();
      if(this.query.type == 'day'){
        this.$router.replace({ path: "/rb-service/oemprofit/service_profit_day" });
      }else if(this.query.type == 'month'){
        this.$router.replace({ path: "/rb-service/oemprofit/service_profit_month" });
      }else if(this.query.type == 'childDay'){
        this.$router.replace({ path: "/rb-service/childOemProfit/service_profit_day" });
      }else if(this.query.type == 'childMonth'){
        this.$router.replace({ path: "/rb-service/childOemProfit/service_profit_month" });
      }else if(this.query.type == 'allDay'){
        this.$router.replace({ path: "/rb-oem/serviceprofit/service_profit_day" });
      }else if(this.query.type == 'allMonth'){
        this.$router.replace({ path: "/rb-oem/serviceprofit/service_profit_month" });
      }else if(this.query.type == 'merDay'){
        this.$router.replace({ path: "/rb-oem/merchantprofit/service_profit_day" });
      }else if(this.query.type == 'merMonth'){
        this.$router.replace({ path: "/rb-oem/merchantprofit/service_profit_month" });
      }else{
        this.$router.back();
      }
    },
    onLoad() {
      this.loading = true;
      let _this = this;
      _this.query = _this.$route.query;

      if(_this.type == 'day'){
        dayList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        dayCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'month'){
        monthList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        monthCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'childDay'){
        childDayList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        childDayCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'childMonth'){
        childMonthList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        childMonthCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'allDay'){
        allDayList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        allDayCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'allMonth'){
        allMonthList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        allMonthCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'merDay'){
        merDayList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        merDayCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }else if(_this.type == 'merMonth'){
        merMonthList(-1,-1,_this.query).then((res) => {
          _this.dataList = res.data.data.records;
          _this.loading = false;
        });

        // 获取统计
        merMonthCount(_this.query).then((res) => {
          _this.countData = res.data.data;
          _this.loading = false;
        });
      }
    },
    
    handleExport() {
      this.$confirm(`是否下载${this.title(this.type)}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if(this.type == 'day'){
          dayExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'month'){
         monthExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'childDay'){
          childDayExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'childMonth'){
          childMonthExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'allDay'){
          allDayExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'allMonth'){
          allMonthExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'merDay'){
          merDayExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }else if(this.type == 'merMonth'){
          merMonthExport(this.query).then((res) => {
              downloadFileExcel(res);
            },
            (error) => {
              window.console.log(error);
            }
          );
        }
      });
    }
  },
};
</script>


<style lang="scss" scoped>
div{
  line-height: 50px;
}
.bgd-f6{
  background-color: #f6f6f6;
}
.header{
  text-align: center;
  box-sizing: border-box;
  width: 100%;
  border:1px solid #ccc;
}
.line{
  width: 100%;
  display: flex;
  justify-content: space-between;
  .line-time{
    width: 15%;
    text-align: center;
    border:1px solid #ccc;
    border-top: none;
    border-right: none;
  }
  .line-time:last-child{
    border-right: 1px solid #ccc;
  }
  // .line-time:nth-child(2),.line-time:nth-child(3),.line-time:nth-child(4){
  //   border-left: none;
  // }
}
.btnFooter{
  display: block;
  margin: 30px auto 10px;
}
</style>